package controller

import (
	"encoding/binary"
	jsoniter "github.com/json-iterator/go"
	"github.com/savsgio/atreugo/v11"
	"homePageTemplate/model"
	"homePageTemplate/service"
	"strconv"
)

type HomePageTemplateController struct {
	HomePageTemplateService *service.HomePageTemplateService
}

func NewHomePageTemplateController() *HomePageTemplateController{
	return &HomePageTemplateController{
		HomePageTemplateService: service.NewHomePageTemplateService(),
	}
}

func (hptc *HomePageTemplateController) EditHomePageDetails(ctx *atreugo.RequestCtx) error {

	var chpd model.CrudHomePageDetails
	chpdByte := ctx.PostBody()
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(chpdByte, &chpd)
	if dErr != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":dErr.Error(), "data":nil})
	}
	err := hptc.HomePageTemplateService.EditHomePageDetails(&chpd)
	if err != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
	}else {
		return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":nil})
	}
}

func (hptc *HomePageTemplateController) EditHomePageDetailsAsync(ctx *atreugo.RequestCtx) error {

	var chpd model.CrudHomePageDetails
	chpdByte := ctx.PostBody()
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(chpdByte, &chpd)
	if dErr != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":dErr.Error(), "data":nil})
	}
	err := hptc.HomePageTemplateService.EditHomePageDetailsAsync(&chpd)
	if err != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
	}else {
		return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":nil})
	}
}

func (hptc *HomePageTemplateController) QueryHomePageDetailsAsync(ctx *atreugo.RequestCtx) error {

	platformIdByte := ctx.FormValue("platformId")
	//platformId := int64(binary.BigEndian.Uint64(platformIdByte))
	platformId, err := strconv.ParseInt(string(platformIdByte), 10, 64)
	data, err := hptc.HomePageTemplateService.QueryHomePageDetailsAsync(platformId)
	if err != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
	}else {
		return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":data})
	}
}

func (hptc *HomePageTemplateController) QueryHomePageDetails(ctx *atreugo.RequestCtx) error {

	platformIdByte := ctx.FormValue("platformId")
	platformId := int64(binary.BigEndian.Uint64(platformIdByte))
	data, err := hptc.HomePageTemplateService.QueryHomePageDetails(platformId)
	if err != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
	}else {
		return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":data})
	}
}

func (hptc *HomePageTemplateController) QueryPageAll(ctx *atreugo.RequestCtx) error {

	data, err := hptc.HomePageTemplateService.QueryPageAll()
	if err != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
	}else {
		return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":data})
	}
}


